package org.zhaya.sdk.swamill;

import cn.hutool.core.util.ClassUtil;
import com.google.common.collect.Maps;
import io.logz.sawmill.*;
import org.zhaya.sdk.DodpProcessor;
import org.zhaya.sdk.util.BdpTemplateService;

import java.util.Map;

/**
 * @author luka
 * @description 初始化注册组件和条件
 * @date 2022/10/10
 */
public class FactoriesRegister {

    public static ProcessorFactoryRegistry processorFactoryRegistry;
    public static ConditionFactoryRegistry conditionFactoryRegistry;

    static {
        registerCustomProcessorsAndCustomConditions();
    }

    /**
     * 注册组件和条件
     */
    public static void registerCustomProcessorsAndCustomConditions() {
        //注册原生processor
        processorFactoryRegistry = new ProcessorFactoryRegistry(
                new ProcessorFactoriesLoader(
                        new TemplateService(), new GeoIpConfiguration("GeoLite2-City.mmdb")
                )
        );
        //注册原生condition
        conditionFactoryRegistry = new ConditionFactoryRegistry(new ConditionalFactoriesLoader(new TemplateService()));
        //动态注册自定义Processor
        Map<Class<?>, Object> dependenciesToInject = Maps.newHashMapWithExpectedSize(2);
        dependenciesToInject.put(BdpTemplateService.class, new BdpTemplateService());
        dependenciesToInject.put(TemplateService.class, new TemplateService());
        // 注册sdk包processor
        new CustomProcessorFactoriesLoader(processorFactoryRegistry, dependenciesToInject, ClassUtil.getPackage(DodpProcessor.class));
        // 注册SPI自定义processor
        new CustomProcessorFactoriesLoader(processorFactoryRegistry, dependenciesToInject);
        //动态注册自定义condition
        new CustomConditionFactoriesLoader(conditionFactoryRegistry, dependenciesToInject);

    }
}
